package com.jiusit.golf.api;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlParser {  
    public static String getHtmlContent(URL url, String encode,String periodStr) {  
        StringBuffer contentBuffer = new StringBuffer();  
  
        int responseCode = -1;  
        HttpURLConnection con = null;  
        String s = "";
        try {  
            con = (HttpURLConnection) url.openConnection();  
            con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");// IE代理进行下载  
            con.setConnectTimeout(60000);  
            con.setReadTimeout(60000);  
            // 获得网页返回信息码  
            responseCode = con.getResponseCode();  
            if (responseCode == -1) {  
                System.out.println(url.toString() + " : connection is failure...");  
                con.disconnect();  
                return null;  
            }  
            if (responseCode >= 400) // 请求失败  
            {  
                System.out.println("请求失败:get response code: " + responseCode);  
                con.disconnect();  
                return null;  
            }  
  
            InputStream inStr = con.getInputStream();  
            InputStreamReader istreamReader = new InputStreamReader(inStr, encode);  
            BufferedReader buffStr = new BufferedReader(istreamReader);  
  
            String str = null;  
            while ((str = buffStr.readLine()) != null)  
                contentBuffer.append(str);  
            inStr.close();  
            s = contentBuffer.toString();
			String data="data-period=\""+periodStr+"\"";
			int index = contentBuffer.indexOf(data) - 50;
			s = contentBuffer.substring(index, index + 90);
			Pattern pattern = Pattern
					.compile("<td[^>]+data-win-number\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
			Matcher matcher = pattern.matcher(s);
			if (matcher.find()) {
				s=matcher.group(1);
				s=s.replaceAll(" ", "");
			}
			else{
				s="";
			}
        } catch (IOException e) {  
            e.printStackTrace();  
            contentBuffer = null;  
            System.out.println("error: " + url.toString());  
        } finally {  
            con.disconnect();  
        }  
        return s;  
    }  
  
    public static String getHtmlContent(String url, String encode,String periodStr) {  
        if (!url.toLowerCase().startsWith("http://")) {  
            url = "http://caipiao.163.com/award/cqssc/" + url + ".html";  
        }  
        try {  
            URL rUrl = new URL(url);  
            return getHtmlContent(rUrl, encode,periodStr);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
    public static void main(String argsp[]){  
    	String s=getHtmlContent("http://caipiao.163.com/award/cqssc/20160304.html","utf-8","160304001");
    	
        System.out.println(s) ;  
          
    } 
}  

